class ItemController < ApplicationController
  
  before_filter :login_required, :except => [:list, :show, :image, :show_restaurant]
  
  verify :method => :post, :only => [:create, :update, :remove, :update_restaurant], :redirect_to => {:action => :list}
  
  def list
    @page, @items = paginate :items, :order => 'created_at DESC', :per_page => 3
  end

  def new
  end

  def new_restaurant
    render :partial => 'restaurant_form' 
  end

  def show
    @item = Item.find(params[:id])
  end

  def edit
    @item = Item.find(params[:id])
  end

  def create
    begin
      create_restaurant
      create_item
    rescue
      log_error($!)
      render :action => :new
      return
    end
    flash[:message] = 'new item successfully created'
    redirect_to :action => :list
  end

  def update
    @item = Item.find params[:id]
    begin
      create_restaurant
      update_item
      delete_pics
    rescue
      log_error($!)
      render :action => :edit
      return
    end
    flash[:message] = 'item successfully updated'
    redirect_to :action => 'show', :id => @item
  end
  
  def delete
    @item = Item.find params[:id]
  end
  
  def remove
    @item = Item.find params[:id]
    @item.destroy
    flash[:message] = 'item successfully delete'
    redirect_to :action => 'list'
  end

  def image
    picture = Picture.find params[:id]
    send_data picture.image, :type => picture.content_type, :disposition => 'inline'
  end


  def show_restaurant
    @restaurant = Restaurant.find(params[:id])
    @page, @items = paginate :items, :conditions => ['restaurant_id=?', @restaurant.id], :order => 'created_at DESC', :per_page => 2
  end

  def edit_restaurant
    @restaurant = Restaurant.find(params[:id])
  end

  def update_restaurant 
    @restaurant = Restaurant.find(params[:id]) 
    unless @restaurant.update_attributes(params[:restaurant]) 
      render :action => :edit_restaurant 
    else 
      flash[:message] = 'restaurant successfully updated'
      redirect_to url_for(:action => :show_restaurant, :id => 
      @restaurant) 
    end 
  end
  
  private
  
  def create_restaurant
    if params[:restaurant]
      @restaurant = Restaurant.new(params[:restaurant])
      @restaurant.save!
      params[:item][:restaurant_id] = @restaurant.id
      @restaurant = nil
    end 
  end

  def create_item
    Item.transaction do
      @item = Item.new(params[:item])
      @item.created_at = param_time()
      @item.save!
      save_pics
    end 
  end
  
  def update_item
    params[:item][:created_at] = param_time()
    Item.transaction do
      unless @item.update_attributes(params[:item])
        raise 'failed to update'
      end
      save_pics
    end
  end

  def save_pics
    [:picture0, :picture1].each do |pic| 
      next if params[pic].nil? || params[pic][:file].length == 0 
      @picture = @item.pictures.build 
      @picture.file = params[pic][:file] 
      @picture.save!
    end 
  end
  
  def delete_pics
    return unless params[:pic]
    params[:pic].each do |k, v|
      Picture.delete k if v == '1'
    end
  end

  def param_time()
    Time.local(params[:date][:year],
    params[:date][:month],
    params[:date][:day],
    params[:date][:hour],
    params[:date][:minute])    
  end
end